import com.ruoyi.framework.web.domain.server.Sys;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PDFReader {
    public static String regex = "¥\\s*\\d+(\\.\\d+)?";
    // 打印文本到控制台
    // System.out.println(text);
    public static Pattern pattern = Pattern.compile(regex);
    public static void main(String[] args) {
        String pdfFilePath = "E:\\doc\\晟勋科技\\收到的发票\\2024-04-06\\digital_24517000000091956291.pdf";
        System.out.println("发票金额："+genMoney(new File(pdfFilePath)));


        double money = 0;
        System.out.println("发票金额：" + money);
        File dir = new File("E:\\doc\\晟勋科技\\收到的发票\\2024-04-06");
        for(File f:dir.listFiles()){
            money = money + genMoney(f);
        }
        System.out.println("发票总金额：" + money);

    }

    public static double genMoney(File file){
        PDDocument document = null;
        try {
            // 加载 PDF 文档
            document = PDDocument.load(file);

            // 创建 PDFTextStripper 实例来提取文本
            PDFTextStripper pdfStripper = new PDFTextStripper();

            // 从 PDF 文档中提取文本
            String text = pdfStripper.getText(document);
         //   System.out.println(text);
            Matcher matcher = pattern.matcher(text);
            double maxNumber = 0;
            while(matcher.find()) {
                String number = matcher.group();
               // System.out.println("提取的数字为：" + number);
                double temp = Double.parseDouble(number.replace("¥","").trim());
                if(temp>maxNumber){
                    maxNumber = temp;
                }
            }
            System.out.println("发票金额：" + maxNumber);
            return maxNumber;
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        } finally {
            // 关闭 PDF 文档
            if (document != null) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
